using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TemplateWeb.Models.Repositories
{
	#region using
	using TemplateWeb.Helpers;
	using TemplateWeb.Models.ViewModels;
	#endregion
	public class VanHoaXaHoiRepository
	{
		public List<VanHoaXaHoi> SelectAll()
		{
			var lstItem = new List<VanHoaXaHoi>();
			var lst = DataContext.Instance.VanHoaXaHois.ToList();
			for (int i = 0; i < lst.Count; i++)
			{
				lstItem.Add(lst[i]);
			}
			return lstItem;
		}

		public VanHoaXaHoi SelectByID(int idnhadat)
		{
			return DataContext.Instance.VanHoaXaHois.FirstOrDefault(item => item.IDNhaDat == idnhadat );
		}

		public List<VanHoaXaHoi> SelectPaging(int page, int pageSize)
		{
			var lstItem = new List<VanHoaXaHoi>();
			var lst = DataContext.Instance.VanHoaXaHois.ToList();
			for (int i = 0; i < lst.Count; i++)
			{
				lstItem.Add(lst[i]);
			}
			if (pageSize > 0)
			{
				int startIndex, itemCount;
				startIndex = (page - 1) * pageSize;
				itemCount = pageSize;
				if (startIndex + itemCount > lstItem.Count)
					itemCount = lstItem.Count - startIndex;
				lstItem = lstItem.GetRange(startIndex, itemCount);
			}
			return lstItem;
		}

		public bool Insert(VanHoaXaHoi obj)
		{
			try
			{
				DataContext.Instance.VanHoaXaHois.InsertOnSubmit(obj);
				DataContext.Instance.SubmitChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public bool Delete(int idnhadat)
		{
			try
			{
				var delitem = DataContext.Instance.VanHoaXaHois.FirstOrDefault(item => item.IDNhaDat == idnhadat );
				DataContext.Instance.VanHoaXaHois.DeleteOnSubmit(delitem);
				DataContext.Instance.SubmitChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public bool Save()
		{
			try
			{
				DataContext.Instance.SubmitChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public int GetTotalItem()
		{
			return DataContext.Instance.VanHoaXaHois.Count();
		}

		public int GetTotalItemByNhaDat(int idnhadat)
		{
			return (from p in DataContext.Instance.VanHoaXaHois where p.IDNhaDat == idnhadat select p).Count();
		}


		public int GetTotalPage()
		{
			int RecordCount = GetTotalItem();
			int PageSize = WebConfiguration.ProductsPerPage;
			return (RecordCount / PageSize) + ((RecordCount % PageSize == 0) ? 0 : 1);
		}

		public int GetTotalPageByNhaDat(int idnhadat)
		{
			int RecordCount = GetTotalItemByNhaDat(idnhadat);
			int PageSize = WebConfiguration.ProductsPerPage;
			return (RecordCount / PageSize) + ((RecordCount % PageSize == 0) ? 0 : 1);
		}


		public List<VanHoaXaHoi> RetrieveByID(int idnhadat)
		{
			var lst = (from item in DataContext.Instance.VanHoaXaHois where  item.IDNhaDat == idnhadat  select item).ToList();
			var lstItem = new List<VanHoaXaHoi>();
			lstItem.Add(lst[0]);
			return lstItem;
		}

		public List<VanHoaXaHoi> SelectByNhaDat(int idnhadat, int page, int pageSize)
		{
			var lstItem = (from item in DataContext.Instance.VanHoaXaHois where item.IDNhaDat == idnhadat select item).ToList();
			if (pageSize > 0)
			{
				int startIndex, itemCount;
				startIndex = (page - 1) * pageSize;
				itemCount = pageSize;
				if (startIndex + itemCount > lstItem.Count)
					itemCount = lstItem.Count - startIndex;
				lstItem = lstItem.GetRange(startIndex, itemCount);
			}
			return lstItem;
		}

	}
}